package com.bupt.kgplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bupt.kgplatform.entity.pojo.SearchInfo;
import com.bupt.kgplatform.mapper.DCollectionMapper;
import com.bupt.kgplatform.service.DCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * ClassName:DCollectionServiceImpl
 * Package:com.example.blackjavaweb.service.impl
 * Description:
 *
 * @Author QIQI_1999
 * @Creat 2023/9/10 16:25
 * @Version 1.0
 */
@Service
public class DCollectionServiceImpl implements DCollectionService {

    @Autowired
    private DCollectionMapper dCollectionMapper;

    @Override
    public Object getMarkInfo(SearchInfo searchInfo) {

        //获取url列表
        List<String> urlLists = dCollectionMapper.getMarkInfoUrl(searchInfo.getLabel());
        HashSet<String> uniqueUrls = new HashSet<>(urlLists);
        urlLists = new ArrayList<>(uniqueUrls);


        //获得信息总数
        //1.获取meetingdata中的会议总数
        Integer meetingNum = dCollectionMapper.getMeetingDataCount(urlLists);
        //2.获取文件总数
        Integer doumentNum = dCollectionMapper.getMeetingDocumentCount(urlLists);

        Integer num = meetingNum + doumentNum;
        if (searchInfo.getPage()* searchInfo.getPagenum()< meetingNum){
            Object meetingList= dCollectionMapper.getMeetingInfoByUrl(urlLists,(searchInfo.getPage()-1) * searchInfo.getPagenum() ,searchInfo.getPagenum());
//            System.out.println("情况1");
//            System.out.println(meetingList);

            Object json = JSONObject.toJSON(meetingList);

            JSONObject mergedObject = new JSONObject();
            mergedObject.put("sum", num);
            mergedObject.put("list1", json);

            return mergedObject;
        } else if ((searchInfo.getPage()-1) * searchInfo.getPagenum()<meetingNum) {
            int start1 = (searchInfo.getPage()-1) * searchInfo.getPagenum();
            int num1 = meetingNum - start1;
            int start2 = searchInfo.getPagenum() - num1;
            Object meetingList = dCollectionMapper.getMeetingInfoByUrl(urlLists, start1, num1);
            Object documentList = dCollectionMapper.getMeetingDocumentByUrl(urlLists, 1, start2);
//            System.out.println("情况2");
//            System.out.println(meetingList);
            Object list1 = JSONObject.toJSON(meetingList);
            Object list2 = JSONObject.toJSON(documentList);

            JSONObject mergedObject = mergeJsonLists(list1, list2);
            mergedObject.put("sum", num);

            return mergedObject;
//            // 将第一个列表的元素添加到合并列表中
//            for (int i = 0; i < list1.size(); i++) {
//                mergedList.add(list1.getJSONObject(i));
//            }
//
//            // 将第二个列表的元素添加到合并列表中
//            for (int i = 0; i < list2.size(); i++) {
//                mergedList.add(list2.getJSONObject(i));
//            }
        }else {
            int start1 =  (searchInfo.getPage()-1) * searchInfo.getPagenum() - meetingNum;
            Object documentList = dCollectionMapper.getMeetingDocumentByUrl(urlLists, start1, searchInfo.getPagenum());
            Object json = JSONObject.toJSON(documentList);

            JSONObject mergedObject = new JSONObject();
            mergedObject.put("sum", num);
            mergedObject.put("list2", json);

            return mergedObject;
        }


    }
    public JSONObject mergeJsonLists(Object list1, Object list2) {
        JSONObject mergedObject = new JSONObject();

        mergedObject.put("list1", list1);
        mergedObject.put("list2", list2);

        return mergedObject;
    }
}
